package cn.stylefeng.guns.modular.wx;

import cn.stylefeng.guns.modular.collect.entity.Collect;
import cn.stylefeng.guns.modular.collect.service.CollectService;
import cn.stylefeng.guns.modular.course.entity.Category;
import cn.stylefeng.guns.modular.course.entity.Course;
import cn.stylefeng.guns.modular.course.entity.CustomerCourse;
import cn.stylefeng.guns.modular.course.service.CategoryService;
import cn.stylefeng.guns.modular.course.service.CourseService;
import cn.stylefeng.guns.modular.course.service.CourseVideoService;
import cn.stylefeng.guns.modular.course.service.CustomerCourseService;
import cn.stylefeng.guns.modular.customer.entity.Customer;
import cn.stylefeng.guns.modular.customer.entity.Order;
import cn.stylefeng.guns.modular.customer.entity.Rebate;
import cn.stylefeng.guns.modular.customer.service.CustomerService;
import cn.stylefeng.guns.modular.customer.service.OrderService;
import cn.stylefeng.guns.modular.customer.service.RebateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/web/my")
@Controller
public class MyController {

    @Autowired
    HttpSession session;

    private String PREFIX = "/web/my";
    @Autowired
    CategoryService categoryService;

    @Autowired
    CourseService courseService;

    @Autowired
    CourseVideoService courseVideoService;

    @Autowired
    CustomerCourseService customerCourseService;

    @Autowired
    OrderService orderService;


    @Autowired
    CustomerService customerService;

    @Autowired
    RebateService rebateService;

    @Autowired
    CollectService collectService;

    /*
    个人中心
     */
    @RequestMapping("my")
    public String my(Model model) {
        Customer customer=(Customer) session.getAttribute("customer");
        //更新下用户数据
        customer=customerService.getById(customer.getId());
        model.addAttribute("customer",customer);

        //获取用户订单数量，课程数量，收藏数量

        Order order=new Order();
        order.setCustomerId(customer.getId());
        List<Order> orderList=orderService.findListBySpec(order);
        model.addAttribute("orderNum",orderList.size());

        Collect collect=new Collect();
        collect.setCustomerId(customer.getId());
        List<Collect> collectList=collectService.findListBySpec(collect);
        model.addAttribute("collectNum",collectList.size());


        CustomerCourse customerCourse=new CustomerCourse();
        customerCourse.setCustomerId(customer.getId());
        List<CustomerCourse> customerCourseList=customerCourseService.findListBySpec(customerCourse);
        model.addAttribute("courseNum",customerCourseList.size());




        return PREFIX + "/my.html";

    }

    /*
    分销中心
     */
    @RequestMapping("center")
    public String center(Model model) {
        Customer customer=(Customer) session.getAttribute("customer");
        //更新下用户数据
        customer=customerService.getById(customer.getId());
        model.addAttribute("customer",customer);

        /*
        上级
         */
        if(customer.getPid()!=null&&customer.getPid()!=0){
            Customer parent=customerService.getById(customer.getPid());
            model.addAttribute("parent",parent);
        }else{
            model.addAttribute("parent",new Customer());
        }



        return PREFIX + "/center.html";

    }

/*
我的团队
 */
    @RequestMapping("team")
    public String team(Model model) {
        Customer customer=(Customer) session.getAttribute("customer");
        //查询儿子
        Customer son=new Customer();
        son.setPid(customer.getId());
        List<Customer> sonList=customerService.findListBySpec(son);
        for(Customer s:sonList){
            //查儿子的儿子
            Customer grandSon=new Customer();
            grandSon.setPid(s.getId());
            List<Customer> grandList=customerService.findListBySpec(grandSon);
            s.setSonList(grandList);

        }
        model.addAttribute("list",sonList);
        System.out.println(sonList);




        return PREFIX + "/team.html";

    }
    /*
    我的课程
     */

    @RequestMapping("myCourse")
    public String myCourse(Model model) {
        Customer customer=(Customer) session.getAttribute("customer");
        model.addAttribute("customer",customer);
        CustomerCourse customerCourse=new CustomerCourse();
        customerCourse.setCustomerId(customer.getId());

        List<CustomerCourse> customerCourseList=customerCourseService.findListBySpec(customerCourse);
        for(CustomerCourse cc:customerCourseList){
            Course course=courseService.getById(cc.getCourseId());
            Category category=categoryService.getById(course.getCategoryId());
            course.setCategoryName(category.getName());
            cc.setCourse(course);

        }
        model.addAttribute("course",customerCourseList);


        return PREFIX + "/my_course.html";
    }

    /**
     * 我的订单
     * @param model
     * @param type
     * @return
     */
    @RequestMapping("myOrder")
    public String myOrder(Model model,Integer state) {
        Customer customer=(Customer) session.getAttribute("customer");
        model.addAttribute("customer",customer);
        Order order=new Order();
        order.setCustomerId(customer.getId());
        order.setState(state);

        List<Order> orderList=orderService.findListBySpec(order);
        for(Order o:orderList){
            Course course=courseService.getById(o.getCourseId());
             o.setCourse(course);

        }
        model.addAttribute("list",orderList);


        return PREFIX + "/my_order.html";
    }



    /*
    分销订单记录,分销积分记录，根据type区分
    传-1查返利和津贴的订单记录，传1查积分记录
     */
    @RequestMapping("rebateOrder")
    public String rebateOrder(Model model,Integer type) {
        Customer customer=(Customer) session.getAttribute("customer");
        Rebate rebate=new Rebate();
        rebate.setCustomerId(customer.getId());
        //-1是查返利和津贴
        rebate.setState(type);
        System.out.println(rebate);
        List<Rebate> rebateList=rebateService.findListBySpec(rebate);
        //查询哪个用户返利的
        for(Rebate r:rebateList){
            Customer c=customerService.getById(r.getBuyerId());
            if(c.getPid().equals(customer.getId())){
                //说明是下级
                r.setLevel("一级");
            }else{
                r.setLevel("二级");
            }
            r.setCustomer(c);


            //查询相关课程
            Course course=courseService.getById(r.getCourseId());
            r.setCourse(course);

        }
        model.addAttribute("list",rebateList);



        return PREFIX + "/rebate_order.html";
    }








}
